#ifndef GLSDLFUNCS_H
#define GLSDLFUNCS_H

#define TRUE (-1)
#define FALSE 0
typedef int BOOL;

BOOL nefInit()
{
    if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
    {
        return FALSE;
    }
    screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL);
    if (screen == NULL)
    {
        return FALSE;
    }
    if (TTF_Init() == -1)
        return FALSE;
    SDL_WM_SetCaption(TITLE, NULL);
    return TRUE;
}

void clean_up()
{
    TTF_Quit();
    SDL_Quit();
    fflush(stdin);
    fflush(stdout);
}

SDL_Surface *load_image(char *filename)
{
    SDL_Surface *loadedImage = NULL;
    SDL_Surface *optimizedImage = NULL;
    loadedImage = IMG_Load(filename);
    if (loadedImage != NULL)
    {
        optimizedImage = SDL_DisplayFormat(loadedImage);
        SDL_FreeSurface(loadedImage);
    }
    return optimizedImage;
}

void SDL_GL_Enter2DMode()
{
    SDL_Surface *screen = SDL_GetVideoSurface();
    glPushAttrib(GL_ENABLE_BIT);
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_CULL_FACE);
    glDisable(GL_LIGHTING);
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glViewport(0, 0, screen->w, screen->h);
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
}

void SDL_GL_Leave2DMode()
{
    glMatrixMode(GL_MODELVIEW);
    glPopMatrix();
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    glPopAttrib();
}

static int power_of_two(int input)
{
    int value = 1;
    while (value < input)
    {
        value <<= 1;
    }
    return value;
}

GLuint SDL_GL_LoadTexture(SDL_Surface *surface, GLfloat *texcoord)
{
    GLuint texture;
    int w, h;
    SDL_Surface *image;
    SDL_Rect area;
    Uint32 saved_flags;
    Uint8 saved_alpha;
    w = power_of_two(surface->w);
    h = power_of_two(surface->h);
    texcoord[0] = 0.0f;
    texcoord[1] = 0.0f;
    texcoord[2] = (GLfloat)surface->w / w;
    texcoord[3] = (GLfloat)surface->h / h;
    image = SDL_CreateRGBSurface(
        SDL_SWSURFACE,
        w, h,
        32,
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
        0x000000FF,
        0x0000FF00,
        0x00FF0000,
        0xFF000000
#else
        0xFF000000,
        0x00FF0000,
        0x0000FF00,
        0x000000FF
#endif
        );
    if (image == NULL)
    {
        return 0;
    }
    saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
#if SDL_VERSION_ATLEAST(1, 3, 0)
    SDL_GetSurfaceAlphaMod(surface, &saved_alpha);
    SDL_SetSurfaceAlphaMod(surface, 0xFF);
#else
    saved_alpha = surface->format->alpha;
    if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA)
    {
        SDL_SetAlpha(surface, 0, 0);
    }
#endif
    area.x = 0;
    area.y = 0;
    area.w = surface->w;
    area.h = surface->h;
    SDL_BlitSurface(surface, &area, image, &area);
#if SDL_VERSION_ATLEAST(1, 3, 0)
    SDL_SetSurfaceAlphaMod(surface, saved_alpha);
#else
    if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA)
    {
        SDL_SetAlpha(surface, saved_flags, saved_alpha);
    }
#endif
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexImage2D(GL_TEXTURE_2D,
                 0,
                 GL_RGBA,
                 w, h,
                 0,
                 GL_RGBA,
                 GL_UNSIGNED_BYTE,
                 image->pixels);
    SDL_FreeSurface(image);
    return texture;
}

#endif